"""
Case Type   : bugfix
Case Name   : sql security语法-存储过程、函数中创建，修改视图权限验证
Create At   : 2025/6/13
Owner       : liu-tong-8848
Description :
    1、B库创建普通用户a
    2、超级用户建表和视图
    3、存储过程中创建视图
    4、超级用户可以执行存储过程
    5、给普通用户a赋予查表权限，调用存储过程报权限错误
    6、清理环境
Expect      :
    1、成功
    2、成功
    3、成功
    4、成功
    5、合理报错
    6、成功
History     :
"""

import os
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class BugFix(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbBmode')
        self.user_node = Node('PrimaryDbBmode')
        self.constant = Constant()

    def test_dolphin(self):
        self.log.info(f'----{os.path.basename(__file__)} start----')
        text = '----step1:B库创建普通用户a和超级用户b；expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop user if exists use_a_1144877; create user use_a_1144877 identified by 'A@123456';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.DROP_ROLE_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertIn(self.constant.CREATE_ROLE_SUCCESS_MSG, sql_res, '执行失败' + text)

        text = '----step2:超级用户建表和视图；expect:成功----'
        self.log.info(text)
        sql_cmd = f"create table sql_security_1144877(id int,cal int); insert into sql_security_1144877 value(1,1); " \
                  + f"insert into sql_security_1144877 value(2,2); insert into sql_security_1144877 value(3,3); " \
                  + f"create schema s_1144877; "
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, sql_res, '执行失败' + text)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertIn(self.constant.CREATE_SCHEMA_SUCCESS_MSG, sql_res, '执行失败' + text)

        sql_cmd = f"create table s_1144877.sql_security_1144877(id int,cal int); " \
                  + f"insert into s_1144877.sql_security_1144877 value(2,1); " \
                  + f"insert into s_1144877.sql_security_1144877 value(3,2); " \
                  + f"insert into s_1144877.sql_security_1144877 value(4,3);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, sql_res, '执行失败' + text)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertNotIn('ERROR', sql_res, '执行失败' + text)

        text = '----step3:存储过程中创建视图；expect:成功----'
        self.log.info(text)
        sql_cmd = f"create or replace procedure p_1144877 as begin " \
                  + f"create sql security invoker view v_1144877 as select * from s_1144877.sql_security_1144877; " \
                  + f"create sql security definer view v_1144877_1 as select * from sql_security_1144877; end;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_PROCEDURE_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertNotIn('ERROR', sql_res, '执行失败' + text)

        text = '----step4:超级用户可以执行存储过程；expect:成功----'
        self.log.info(text)
        sql_cmd = f"call p_1144877(); select * from v_1144877 order by 1,2;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('(3 rows)', sql_res, '执行失败' + text)
        self.assertNotIn('ERROR', sql_res, '执行失败' + text)

        sql_cmd = f"select * from v_1144877_1 order by 1,2;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('(3 rows)', sql_res, '执行失败' + text)
        self.assertNotIn('ERROR', sql_res, '执行失败' + text)

        text = '----step5:给普通用户a赋予查表权限，调用存储过程报权限错误；expect:合理报错----'
        self.log.info(text)
        sql_cmd = f"grant select on v_1144877 to use_a_1144877; grant select on v_1144877_1 to use_a_1144877; " \
                  + f"grant all on table s_1144877.sql_security_1144877 to use_a_1144877;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.GRANT_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertNotIn('ERROR', sql_res, '执行失败' + text)

        sql_cmd = f"set role use_a_1144877 password 'A@123456'; select * from v_1144877 order by 1,2; " \
                  + f"select * from v_1144877_1 order by 1,2;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertIn('(3 rows)', sql_res, '执行失败' + text)
        self.assertIn('ERROR', sql_res, '执行失败' + text)
        self.assertIn('permission denied for schema s_1144877', sql_res, '执行失败' + text)

    def tearDown(self):
        text = '----step6:清理环境；expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop table if exists sql_security_1144877 cascade; drop schema if exists s_1144877 cascade; " \
                  + f"drop view if exists v_1144877_1; drop user if exists use_a_1144877 cascade;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, sql_res, '执行失败' + text)
        self.assertIn(self.constant.DROP_SCHEMA_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertIn(self.constant.DROP_VIEW_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertIn(self.constant.DROP_ROLE_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.log.info(f'----{os.path.basename(__file__)} end-----')
